import axios from 'axios'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'


// 创建axios实例
let $axios = axios.create({
  timeout: 10000, // 设置超时时间10s
  withCredentials: true, // 跨域凭证
})

// 请求拦截器
$axios.interceptors.request.use(config =>{
  // 使用进度条
  NProgress.start();
  // 获取token
  let token = window.sessionStorage.getItem('token')
  if(token) config.headers.token = token;
  return config;
},error =>{
  return Promise.reject(error)
})

// 响应拦截器
$axios.interceptors.response.use(response =>{
  NProgress.done();
  return response;
},error=>{
  return Promise.reject(error)
})


export default $axios;